<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="appPath" scope="application" value="${pageContext.request.contextPath}"/>

<script>

/**檔案上傳**/
function doAction(){
	var f = document.getElementById("AgricultureAndFoodForm");
	f.action = "${appPath }/form/files/upload.htm";	
	if(f.fileData.value.length != 0){		
		//***判斷是否為pdf檔*******************
		var str = f.fileData.value;
		var patt=/\.(pdf|doc|docx|mht|docm)$/gi;
		if(!patt.test(str)){
			showMessage("限制上傳檔案為PDF 、MS-WORD、MHT及DOCM格式",2);
			f.fileData.outerHTML=f.fileData.outerHTML.replace(/(value=\").+\"/i,"$1\"");
			return;
		}
		//***********************************		
		$('#AgricultureAndFoodForm').ajaxSubmit({ 
			data: $('#AgricultureAndFoodForm').serialize() ,
			dataType: 'json',
			success: function(data2) {			
				//alert(">> " + data2);
				data2 = data2.substring(0,data2.length-1);				
				var seqno = data2.split(":")[0];
				var name  = data2.split(":")[1];
				//var size  = data2.split(":")[2];				
				var remove = "<span class='editField'><a href='javascript:removefile("+seqno+");'>( 移除 )</a></span>";				
				var _f = "<div id='files_"+seqno+"'>" + name + "&nbsp " + remove + "</div>";				 
				$("#upload_files_area").append(_f);				
				f.fileData.outerHTML=f.fileData.outerHTML.replace(/(value=\").+\"/i,"$1\"");				
				if($("#files").val().length == 0)
					$("#files").val(",");
				$("#files").val($("#files").val() + seqno + ",");
				$("#_ofiles").css({border:"0px solid #FFFFFF"});
			}
		});
	}else{
		showMessage("請選擇檔案",2);
	}
}

/**表單比對**/
function comparison(){
	var _t = trim($("#title_o").val());
	var _a  = trim($("#author").val());
	hideMessage("slow");
	if(_t.length != 0 && _a.length != 0){
		$.post("${appPath}/form/comparison.htm",{title_o:_t,author:_a},function(data){
			if(data.length == 6){
				var msg = "表單重覆 :&nbsp;<a href='javascript:void(0);' onclick='preview(\""+data+"\");'>" + data + "(檢視)</a>";
				showMessage(msg,9);			
				//$("#MSG_TABLE").unbind('click.msg');
			}
		});
	}
}

function hideMessage(speed){	
	$('#messageBox').fadeOut(speed);
}

function showMessage(msg,state){
	var _img = "";
	switch(state){
	case 1:
		_img = "<img src='${appPath}/resource/image/1317488036_ok.gif' />&nbsp;";
		break;
	case 2:
		_img = "<img src='${appPath}/resource/image/1317500114_attention1.gif' />&nbsp;";
		break;
	case 3:
		_img = "<img src='${appPath}/resource/image/1317488201_connect3.gif' />&nbsp;";
		break;
	case 4: /*用戶新增*/
		_img = "<img src='${appPath}/resource/image/1317498616_user3_add.gif' />&nbsp;";
		break;
	case 5:	/*用戶修改*/
		_img = "<img src='${appPath}/resource/image/1317498623_user2_edit.gif' />&nbsp;";
		break;
	case 6:	/*用戶刪除*/
		_img = "<img src='${appPath}/resource/image/1317498625_user2_del.gif' />&nbsp;";
		break;
	case 7:	/*loading*/
		_img = "<img src='${appPath}/resource/image/ajax-loader.gif' />&nbsp;";
		break;	
	case 8:	/*send mail*/
		_img = "<img src='${appPath}/resource/image/1318420164_mail-send-receive.png' />&nbsp;";
		break;	
	case 9:	/*form repeat*/
		_img = "<img src='${appPath}/resource/image/library.png' />&nbsp;";
		break;	
	}	
	$("#message").html(_img + msg);
	$("#messageBox").fadeIn('slow');	
	if(state != 2 && state != 7 && state != 9){
		$("#close_msg").hide();
		setTimeout("$('#messageBox').fadeOut('slow')", 5000 );
	}else if(state == 2 || state == 9){
		$("#close_msg").show();
		//setTimeout("$('#messageBox').fadeOut('slow')", 60000 );
	}else if(state == 7){
		$("#close_msg").hide();
	}
}

function showClose(n){
	var width = n == 1 ? "30px" : "2px";
	var msg = n == 1 ? "關閉" : "";
	$("#close_msg").css("width",width).html(msg);	
}

function removeCountryCode(code,name_ch){
	var countriesCode = $("#countries_code").val();
	countriesCode = countriesCode.replace(code + "," ,"");
	var countriesNameCh = $("#countries_name_ch").val();
	countriesNameCh = countriesNameCh.replace(name_ch + "," ,"");	
	$("#countries_code").val(countriesCode);
	$("#countries_name_ch").val(countriesNameCh);
	$("#CC_" + code).remove();
}

function removePlantCode(code,name_ch){
	var plantsCode = $("#plants_code").val();
	plantsCode = plantsCode.replace(code + "," ,"");
	var plantsNameCh = $("#plants_name_ch").val();
	plantsNameCh = plantsNameCh.replace(name_ch + "," ,"");	
	$("#plants_code").val(plantsCode);
	$("#plants_name_ch").val(plantsNameCh);
	$("#CP_" + code).remove();
}

function getCodeCountryQuery(qr,isInit){
	$("#countryLoadingImg").show();	
	$("#countryFilterLoadingImg").show();
	var _region = "";	
	var _countriesCode = "";
	var _countriesNameCh = "";	
	if(isInit){
		$("#selectedArea").html("");
		$("#qr").val("");		
		_countriesCode = $("#countries_code").val();
		_countriesNameCh = $("#countries_name_ch").val();		
		for(var i = 0 ; i < _countriesCode.split(",").length - 1 ; i++){
			selectedCountryInit(_countriesCode.split(",")[i],_countriesNameCh.split(",")[i]);
		}
	}else{
		$('.regions').each(function(){		
			if($(this).prop("checked")){
				_region += this.value + ",";
			}
		});
		_countriesCode = $("#selectedCodeCountries").val();
		_countriesNameCh = $("#selectedNameChCountries").val();
	}	
	$.post("${appPath}/form/code/country/query.htm",{qr:qr,region:_region,isInit:isInit,countriesCode:_countriesCode,countriesNameCh:_countriesNameCh},function(data){
		$("#OverlayCountryContent").html(data);
		$("#HintCountryOverlay").show();
		$("#countryLoadingImg").hide();	
		$("#countryFilterLoadingImg").hide();
	});
}

function getCodePlantQuery(qr,isInit){
	$("#plantLoadingImg").show();
	$("#plantFilterLoadingImg").show();	
	var name_ch,_plantsSelected;
	if(isInit){
		$("#selectedPlantArea").html("");
		$("#qr_plant").val('');
		_plantsSelected = $("#plants_code").val();
		_plants_ch = $("#plants_name_ch").val();
		for(var i = 0 ; i < _plantsSelected.split(",").length - 1 ; i++){
			selectedPlantInit(_plantsSelected.split(",")[i],_plants_ch.split(",")[i]);
		}
		name_ch = $("#plants_name_ch").val();		
	}else{
		_plantsSelected = $("#selectedCodePlants").val();	
		name_ch = $("#selectedCodePlantsNameCH").val();
	}
	$.post("${appPath}/form/code/plant/query.htm",{qr:qr,name_ch:name_ch,isInit:isInit,plantsSelected:_plantsSelected},function(data){
		$("#OverlayPlantContent").html(data);
		$("#HintPlantOverlay").show();
		$("#plantLoadingImg").hide();
		$("#plantFilterLoadingImg").hide();
	});
}

function view(seq_no,state){
	showMessage("",7);	
	$("#loading_1").show();

	$.post("${appPath}/form/page/own/form.htm",{seq_no:seq_no,state:state},function(data){	
		$("#DIS_ROWPERPAGE").fadeOut("fast");
		$("#querylist").fadeOut("fast",function(){		
			$("#view").html(data).fadeIn("fast");	
			$("#loading_1").fadeOut("fast");
		});		
		hideMessage("fast");
	});
	
	
}

function backlist(){	
	$('#messageBox').fadeOut('slow');
	$("#view").fadeOut("fast",function(){
		$("#querylist").fadeIn("fast");
		$("#DIS_ROWPERPAGE").fadeIn("fast");
	});	
}

function selectedCountryInit(code,name_ch){
	var rem = "";
	var _a = '<span style="border: dotted 3px #0000FF;background-color:#FFFFFF;padding:5px;" class="spanSelected_'+code+'" >';
	//_a += code + rem + "</span><span class=\"spanSelected_"+code+"\">&nbsp;&nbsp;</span>";		//* display code */
	_a += name_ch + rem + "</span><span class=\"spanSelected_"+code+"\">&nbsp;&nbsp;</span>";		//* display name_ch */
	$("#selectedArea").append(_a);
}

function selectedPlantInit(code,name_ch){	
	var rem = "";
	var _a = '<span style="border: dotted 3px #0000FF;background-color:#FFFFFF;padding:5px;" class="spanSelected_'+code+'" >';
	_a += name_ch + rem + "</span><span class=\"spanSelected_"+code+"\">&nbsp;&nbsp;</span>";
	$("#selectedPlantArea").append(_a);	
}

function removefile(id){
	$("#files_" + id).remove();
	var _files = $("#files").val().replace(id + "," , "");
	$("#files").val(_files);
}

function checkClazz(){
	var _cnt = 0;	
	$('.clazz').each(function(){			
		if($(this).prop("checked")){
			$("#_clazz").css({border:"0px solid #FFFFFF"});
			_cnt++;			
			$('.clazz').each(function(){	
				if(!$(this).prop("checked")){
					$(this).attr("disabled",(_cnt >= 3));
				}
			});
		}
	});
}

/*
function selectedPlant(code,name_ch){
	var rem = "";
	var _a = '<span style="border: dotted 3px #0000FF;background-color:#FFFFFF;padding:5px;" class="spanSelected_'+code+'" >';
	_a += name_ch + rem + "</span><span class=\"spanSelected_"+code+"\">&nbsp;&nbsp;</span>";
	$("#selectedPlantArea").append(_a);
	$("#dis_" + code)
		.css({"color":"red"})
		.unbind("click")
		.bind("click","disSelectedPlant('"+code+"','"+name_ch+"');");
	var scc = $("#selectedCodePlants").val();
	scc += code + ",";
	$("#selectedCodePlants").val(scc);
	$("#selectedCodePlantsNameCH").val($("#selectedCodePlantsNameCH").val() + name_ch + ",");
}
*/




</script>

